﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_ExportQuotation : UserControl
    {
        public panel_ExportQuotation()
        {
            InitializeComponent();
            LoadData();
            this.ExportQuotation.PopupMenuShowing += ExportQuotation_PopupMenuShowing;
            this.exportQuotationContext.ItemClicked += exportQuotationContext_ItemClicked;
            this.ExportQuotation.KeyPress += ExportQuotation_KeyPress;
            this.ExportQuotation.DoubleClick += ExportQuotation_DoubleClick;
        }

        ///////////////////////////////////////////
        /// Call Edit Form
        //////////////////////////////////////////
        private void callEditForm()
        {
            DataRow row = ExportQuotation.GetFocusedDataRow();
            frm_Edit_ExportQuotation edit_form = new frm_Edit_ExportQuotation();
            edit_form.oldid = row["QuotationID"].ToString();
            if (edit_form.LoadData())
            {
                //edit_form.Show();
                edit_form.ShowDialog();
                LoadData();
            }
            else
            {
                edit_form.Dispose();
                MessageBox.Show("No Quotation to edit");
            }
        }

        void ExportQuotation_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();
        }

        void ExportQuotation_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                callEditForm();
            }
        }

        void exportQuotationContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.exportQuotationContext.Hide();
            DataRow row = ExportQuotation.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_ExportQuotation add_Form = new frm_Add_ExportQuotation();
                    //add_Form.Show();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void ExportQuotation_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.exportQuotationContext.Show(MousePosition);            
        }

        ///////////////////////////////////////
        /// Load Data to Grid
        ///////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySetting = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [241_ExportQuotation].QuotationID, [222_Customer].FullName, [241_ExportQuotation].Inputdate,"
                + " Staff1.StaffName, Staff2.StaffName, [241_ExportQuotation].ComOrPart, [241_ExportQuotation].ByQuantity,"
                + " [222_Currency].CurrencySymbol, [241_ExportQuotation].Validity, [241_ExportQuotation].BankInfor,"
                + " [241_ExportQuotation].RateExchange, [241_ExportQuotation].Note"
                + " FROM [241_ExportQuotation] LEFT OUTER JOIN"
                + " [222_Customer] ON [222_Customer].CustomerID=[241_ExportQuotation].CustomerID LEFT OUTER JOIN"
                + " [222_Staff] Staff1 ON Staff1.StaffID=[241_ExportQuotation].RequestedBy LEFT OUTER JOIN"
                + " [222_Staff] Staff2 ON Staff2.StaffID=[241_ExportQuotation].MadeBy LEFT OUTER  JOIN"                
                + " [222_Currency] ON [241_ExportQuotation].CurrencyID=[222_Currency].CurrencyID";
            sourceDataSet = mySQLManagement.SQL_Query(sql);            
            if (sourceDataSet != null)
            {
                gridExportQuotation.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Export Quotation");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("QuotationID", typeof(string));
                newTable.Columns.Add("Customer", typeof(string));
                newTable.Columns.Add("InputDate", typeof(DateTime));
                newTable.Columns.Add("RequestedBy", typeof(string));
                newTable.Columns.Add("MadeBy", typeof(string));
                newTable.Columns.Add("ComOrPart", typeof(bool));
                newTable.Columns.Add("ByQuantity", typeof(bool));
                newTable.Columns.Add("Currency", typeof(string));
                newTable.Columns.Add("Validity", typeof(bool));                
                newTable.Columns.Add("BankInfor", typeof(string));
                newTable.Columns.Add("RateExchange", typeof(float));
                newTable.Columns.Add("Note", typeof(string));
                gridExportQuotation.DataSource = newTable;
            }
            ExportQuotation.Columns[0].Caption = "Quotation ID";
            ExportQuotation.Columns[1].Caption = "Customer";
            ExportQuotation.Columns[2].Caption = "Input Date";
            ExportQuotation.Columns[3].Caption = "Requested By";
            ExportQuotation.Columns[4].Caption = "Made By";
            ExportQuotation.Columns[5].Caption = "Comp. Quotation";
            ExportQuotation.Columns[6].Caption = "By Quantity";
            ExportQuotation.Columns[7].Caption = "Currency";
            ExportQuotation.Columns[8].Caption = "Validity";
            ExportQuotation.Columns[9].Caption = "Bank Account";
            ExportQuotation.Columns[10].Caption = "Rate Exchange";            
            ExportQuotation.Columns[11].Caption = "Note";
            ExportQuotation.BestFitColumns();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_ExportQuotation add_Form = new frm_Add_ExportQuotation();
            //add_Form.Show();
            add_Form.ShowDialog();
            LoadData();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }
    }
}
